package com.vim.modules.web.service;

import com.vim.common.base.Constants;
import com.vim.common.base.CrudServiceImpl;
import com.vim.common.utils.IdGen;
import com.vim.modules.web.dao.SysUserMapper;
import com.vim.modules.web.model.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

@Service
public class SysUserService extends CrudServiceImpl<SysUser> {

    @Resource
    private SysUserMapper sysUserMapper;

    @Autowired
    public SysUserService(SysUserMapper mapper) {
        super(mapper);
    }

    @Override
    public int save(SysUser entity) {
        entity.setUserId(IdGen.uuid());
        entity.setLoginPassword(IdGen.password(entity.getLoginAccount(), Constants.initPassword));
        return super.save(entity);
    }

    @Override
    public SysUser find(String id) {
        SysUser sysUser = super.find(id);
        if(null != sysUser){
            sysUser.setLoginPassword("");
        }
        return sysUser;
    }

    /**
     * 根据登录账号查询用户
     * @param loginAccount
     */
    public SysUser findLoginAccount(String loginAccount) {
        return sysUserMapper.findLoginAccount(loginAccount);
    }

    /**
     * 删除用户，包括角色权限信息
     * @param id
     */
    @Transactional(rollbackFor = Exception.class)
    public void deleteUser(String id) {
        sysUserMapper.delete(id);
        sysUserMapper.deleteUserRole(id);
    }
}

